<template>
  <div class="app-container">
    <!-- 顶部导航栏 -->
    <header class="navbar">
      <div class="logo" @click="toHome">
        <svg class="music-icon" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
          <path
            d="M12 3L14.5 7H19V17H14.5L12 21L9.5 17H5V7H9.5L12 3Z"
            stroke="currentColor"
            stroke-width="2"
            stroke-linecap="round"
            stroke-linejoin="round"
          />
        </svg>
        <span>MusicShare</span>
      </div>
      <div class="search-bar">
        <input type="text" placeholder="搜索还没写" />
        <button><i class="el-icon-search"></i></button>
      </div>
      <div class="user-actions">
        <button v-if="!isLoggedIn" @click="toLogin">登录</button>
        <button v-if="!isLoggedIn" @click="toRegister">注册</button>
        <div v-else class="user-actions-logged">
          <MessageNotification />
          <div class="user-info">
            <img
              :src="
                userInfo && userInfo.avatarUrl
                  ? userInfo.avatarUrl
                  : 'https://sky-take-ou7.oss-cn-beijing.aliyuncs.com/default.jpeg'
              "
              alt="用户头像"
            />
            <span>{{ userInfo?.nickname || '未命名' }}</span>
            <div class="dropdown-menu">
              <button @click="toProfile">个人中心</button>
              <button @click="logout">退出登录</button>
            </div>
          </div>
        </div>
      </div>
    </header>

    <!-- 主内容区域 -->
    <main class="main-content">
      <!-- 返回按钮容器 - 遵循1200px宽度限制 -->
      <div class="back-btn-section" v-if="!isHomePage">
        <button class="back-btn-sticky" @click="goBack" title="返回上一页">
          <svg class="back-icon" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
            <path
              d="M15 18L9 12L15 6"
              stroke="currentColor"
              stroke-width="2"
              stroke-linecap="round"
              stroke-linejoin="round"
            />
          </svg>
          返回
        </button>
      </div>
      <router-view />
    </main>

    <!-- 底部信息 -->
    <footer class="footer">
      <div class="footer-content">
        <div class="footer-info">
          <h3>Music Share</h3>
          <p class="project-desc">
            一个专注于音乐分享的开发项目，支持音乐上传、搜索和播放功能，旨在为音乐爱好者提供高效稳定的在线服务。
          </p>
        </div>
        <div class="footer-links">
          <h4>项目仓库</h4>
          <ul>
            <li>
              <a href="https://gitee.com/tudouza1/music-share-ui" target="_blank" rel="noopener"
                >用户端</a
              >
            </li>
            <li>
              <a
                href="https://gitee.com/tudouza1/music-share-backend"
                target="_blank"
                rel="noopener"
                >后端项目</a
              >
            </li>
            <li>
              <a href="https://gitee.com/tudouza1" target="_blank" rel="noopener">Gitee主页</a>
            </li>
          </ul>
        </div>
      </div>
      <div class="footer-copyright">
        <p>© 2025 MusicShare. Built with ❤️ for music lovers</p>
      </div>
    </footer>
  </div>
</template>

<script setup>
import { computed } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { useUserStore } from '../stores/user'
import MessageNotification from '../components/MessageNotification.vue'

const router = useRouter()
const route = useRoute()
const userStore = useUserStore()
const isLoggedIn = computed(() => userStore.isLoggedIn)
const userInfo = computed(() => userStore.getUserInfo)

// 判断是否首页
const isHomePage = computed(() => route.path === '/')

// 返回上一页
const goBack = () => {
  if (window.history.length > 1) {
    router.go(-1)
  } else {
    router.push('/')
  }
}

const toHome = () => {
  router.push('/')
}

const toLogin = () => {
  router.push('/login')
}

const toRegister = () => {
  router.push('/register')
}

const toProfile = () => {
  if (userStore.userInfo?.id) {
    router.push(`/profile/${userStore.userInfo.id}`)
  } else {
    router.push('/login')
  }
}

const logout = () => {
  userStore.logout()
  router.push('/')
}
</script>

<style scoped>
/* 基础样式 */
:root {
  --primary-color: #1db954;
  --secondary-color: #2d46b9;
  --text-color: #333;
  --light-text: #666;
  --background-color: #f5f5f7;
  --card-background: #ffffff;
  --border-radius: 8px;
  --shadow: 0 4px 12px rgba(0, 0, 0, 0.08);
}

.app-container {
  display: flex;
  flex-direction: column;
  min-height: 100vh;
  background-color: var(--background-color);
}

/* 导航栏样式 */
.navbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0 2rem;
  height: 70px;
  background-color: rgba(255, 255, 255, 0.9);
  backdrop-filter: blur(12px);
  border-bottom: 1px solid rgba(0, 0, 0, 0.05);
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  z-index: 100;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.03);
  box-sizing: border-box;
}

.logo {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  font-size: 1.6rem;
  font-weight: 700;
  color: var(--primary-color);
  transition: transform 0.2s ease;
  height: 100%;
  cursor: pointer;
}

.logo:hover {
  transform: scale(1.03);
}

.music-icon {
  width: 30px;
  height: 30px;
  color: var(--primary-color);
}

.search-bar {
  display: flex;
  align-items: center;
  background-color: rgba(0, 0, 0, 0.05);
  border-radius: 30px;
  padding: 0.75rem 1.25rem;
  width: 45%;
  transition: all 0.3s ease;
  height: 40px;
  box-sizing: border-box;
}

.search-bar:hover {
  background-color: rgba(0, 0, 0, 0.08);
}

.search-bar input {
  flex: 1;
  background: transparent;
  border: none;
  outline: none;
  color: var(--text-color);
  font-size: 1rem;
}

.search-bar button {
  background: transparent;
  border: none;
  color: var(--light-text);
  cursor: pointer;
  transition: color 0.3s ease;
}

.search-bar button:hover {
  color: var(--primary-color);
}

.user-actions {
  display: flex;
  align-items: center;
  gap: 1rem;
  height: 100%;
}

.user-actions button {
  padding: 0.6rem 1.25rem;
  border: none;
  border-radius: 30px;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.3s ease;
}

.user-actions button:nth-child(1) {
  background-color: transparent;
  color: var(--text-color);
}

.user-actions button:nth-child(2) {
  background-color: var(--primary-color);
  color: white;
  box-shadow: 0 4px 10px rgba(29, 185, 84, 0.2);
}

.user-actions button:nth-child(2):hover {
  transform: translateY(-2px);
  box-shadow: 0 6px 14px rgba(29, 185, 84, 0.3);
}

.user-actions button:nth-child(2):active {
  transform: translateY(0);
  box-shadow: 0 2px 6px rgba(29, 185, 84, 0.2);
}

.user-actions-logged {
  display: flex;
  align-items: center;
  gap: 1rem;
}

.user-info {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  cursor: pointer;
  position: relative;
  padding: 0.5rem;
  border-radius: 30px;
  transition: background-color 0.3s ease;
}

.user-info:hover {
  background-color: rgba(0, 0, 0, 0.03);
}

.user-info img {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  object-fit: cover;
  border: 2px solid var(--primary-color);
  transition: transform 0.3s ease;
}

.user-info:hover img {
  transform: scale(1.05);
}

.user-info span {
  color: var(--text-color);
  font-weight: 500;
  font-size: 0.95rem;
}

.dropdown-menu {
  position: absolute;
  top: 50px;
  right: 0;
  background-color: white;
  border-radius: var(--border-radius);
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.05);
  /* padding: 0.5rem 0; */
  min-width: 140px;
  z-index: 1000;
  display: none;
  transform: translateY(10px);
  opacity: 0;
  transition: all 0.5s ease;
  border: 1px solid rgba(0, 0, 0, 0.05);
}

.user-info:hover .dropdown-menu {
  display: block;
  transform: translateY(0);
  opacity: 1;
  transition: all 0.5s ease;
}

.dropdown-menu button {
  width: 100%;
  text-align: left;
  padding: 0.6rem 1.25rem;
  background-color: transparent;
  border: none;
  cursor: pointer;
  transition: all 0.2s ease;
  font-size: 0.95rem;
  display: flex;
  align-items: center;
  gap: 0.5rem;
  border-radius: 0px;
}

.dropdown-menu button:hover {
  background-color: rgba(0, 0, 0, 0.05);
  color: var(--primary-color);
  /* padding-left: 1.4rem; */
}

/* 主内容区域样式 */
.main-content {
  flex: 1;
  padding: 2rem;
  margin-top: 70px; /* 避免被导航栏遮挡 */
  margin-bottom: 70px;
}

/* 底部样式优化 */
.footer {
  padding: 3rem 2rem 2rem;
  background: linear-gradient(135deg, #f8f8f8 0%, #ffffff 100%);
  color: #333;
  border-top: 1px solid #e8e8e8;
  margin-top: auto;
}

.footer-content {
  display: grid;
  grid-template-columns: 2fr 1fr;
  gap: 3rem;
  max-width: 1200px;
  margin: 0 auto 2rem;
}

.footer-info h3 {
  font-size: 1.5rem;
  color: var(--primary-color);
  margin-bottom: 1rem;
  font-weight: 700;
}

.project-desc {
  font-size: 0.95rem;
  line-height: 1.7;
  color: #666;
  max-width: 400px;
}

.footer-links h4 {
  font-size: 1.1rem;
  margin-bottom: 1rem;
  color: #333;
  font-weight: 600;
}

.footer-links ul {
  list-style: none;
  padding: 0;
}

.footer-links li {
  margin-bottom: 0.5rem;
}

.footer-links a {
  color: #666;
  text-decoration: none;
  transition: all 0.3s ease;
  font-size: 0.9rem;
  padding: 0.25rem 0;
  display: inline-block;
}

.footer-links a:hover {
  color: var(--primary-color);
  transform: translateX(2px);
}

.footer-copyright {
  text-align: center;
  font-size: 0.85rem;
  color: #999;
  padding-top: 1.5rem;
  border-top: 1px solid #e8e8e8;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .footer-content {
    grid-template-columns: 1fr;
    gap: 2rem;
    text-align: center;
  }

  .project-desc {
    max-width: 100%;
  }

  .footer {
    padding: 2rem 1rem 1.5rem;
  }
}

/* 移动端导航栏优化 */
@media (max-width: 768px) {
  .navbar {
    padding: 0 0.5rem;
    height: 60px;
  }

  .logo {
    font-size: 1.2rem;
    margin-right: 0.5rem;
  }

  .logo-icon {
    width: 24px;
    height: 24px;
  }

  .search-bar {
    flex: 0 1 120px;
    margin: 0 0.5rem;
    max-width: 150px;
  }

  .search-input {
    padding: 0.5rem 0.5rem;
    font-size: 0.85rem;
    height: 36px;
    min-width: 80px;
  }

  .search-btn {
    padding: 0.4rem;
    height: 36px;
    width: 36px;
  }

  .user-actions {
    gap: 0.5rem;
    margin-left: 0.5rem;
  }

  .user-actions .btn {
    padding: 0.4rem 0.6rem;
    font-size: 0.8rem;
    height: 36px;
  }

  .user-info {
    padding: 0.4rem 0.6rem;
    height: 36px;
  }

  .avatar {
    width: 28px;
    height: 28px;
  }

  .main-content {
    padding: 1rem;
    margin-top: 60px;
    margin-bottom: 80px;
  }
}

@media (max-width: 480px) {
  .navbar {
    padding: 0 0.3rem;
    height: 55px;
  }

  .logo {
    font-size: 1.1rem;
  }

  .logo-icon {
    width: 22px;
    height: 22px;
  }

  .search-bar {
    flex: 0 1 80px;
    margin: 0 0.3rem;
    max-width: 100px;
  }

  .search-input {
    padding: 0.4rem 0.4rem;
    font-size: 0.8rem;
    height: 34px;
    min-width: 60px;
  }

  .search-btn {
    padding: 0.3rem;
    height: 34px;
    width: 34px;
  }

  .user-actions {
    gap: 0.3rem;
    margin-left: 0.3rem;
  }

  .user-actions .btn {
    padding: 0.3rem 0.5rem;
    font-size: 0.75rem;
    height: 34px;
  }

  .user-info {
    padding: 0.3rem 0.5rem;
    height: 34px;
  }

  .avatar {
    width: 26px;
    height: 26px;
  }

  .main-content {
    padding: 0.8rem;
    margin-top: 55px;
    margin-bottom: 75px;
  }
}
/* 返回按钮 - sticky定位 */
.back-btn-section {
  max-width: 1200px;
  margin: 0 auto;
  padding: 1rem;
  position: sticky;
  top: 70px;
  z-index: 99;
}

.back-btn-sticky {
  /* left: 60px; */
  display: inline-flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.75rem 1.25rem;
  background: rgba(255, 255, 255, 0.65);
  border: 1px solid rgba(29, 185, 84, 0.2);
  border-radius: 50px;
  color: var(--primary-color);
  font-size: 0.9rem;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.3s ease;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.08);
  backdrop-filter: blur(10px);
  white-space: nowrap;
}

.back-btn-sticky:hover {
  background: var(--primary-color);
  color: white;
  transform: translateY(-2px);
  box-shadow: 0 4px 20px rgba(29, 185, 84, 0.3);
  border-color: var(--primary-color);
}

.back-icon {
  width: 16px;
  height: 16px;
  transition: transform 0.3s ease;
}

.back-btn-sticky:hover .back-icon {
  transform: translateX(-2px);
}

/* 响应式设计 - 返回按钮 */
@media (max-width: 768px) {
  .back-btn-sticky {
    top: 80px;
    left: 15px;
    padding: 0.6rem 1rem;
    font-size: 0.85rem;
  }

  .back-icon {
    width: 14px;
    height: 14px;
  }
}

@media (max-width: 480px) {
  .back-btn-sticky {
    top: 75px;
    left: 10px;
    padding: 0.5rem 0.875rem;
    font-size: 0.8rem;
  }
}
</style>
